//
//  THShareViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/17.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THShareViewController.h"

@interface THShareViewController ()
@property(nonatomic, strong) STButton                     *stoneobs_ui_clearButton;
@property(nonatomic, strong) UIView                     *stoneobs_ui_customView;
@end

@implementation THShareViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self stoneobs_ui_configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self stoneobs_fun_show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)stoneobs_ui_configSubView{
    self.view.backgroundColor = UIColor.clearColor;
    __weak typeof(self) weakSelf =  self;
    self.stoneobs_ui_clearButton = [[STButton alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME
                                                             title:nil
                                                        titleColor:nil
                                                         titleFont:0
                                                      cornerRadius:0
                                                   backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                                   backgroundImage:nil
                                                             image:nil];
    [weakSelf.stoneobs_ui_clearButton setClicAction:^(UIButton *sender) {
        [weakSelf stoneobs_fun_dismiss];
    }];
    [self.view addSubview:_stoneobs_ui_clearButton];
    
    self.stoneobs_ui_customView = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    self.stoneobs_ui_customView.centerX = UISCREEN_WIDTH / 2;
    self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
    self.stoneobs_ui_customView.backgroundColor = [UIColor whiteColor];
    self.stoneobs_ui_customView.layer.cornerRadius = 10;
    self.stoneobs_ui_customView.clipsToBounds = YES;
    [self.view addSubview:self.stoneobs_ui_customView];
    
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 30, STONEOBS_UI_UISCREEN_WIDTH , 20)
                                                            text:@"分享至"
                                                       textColor:UIColor.blackColor
                                                            font:15
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentCenter];
    [self.stoneobs_ui_customView addSubview:stoneobs_ui_label];
    stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:20];
    
    
    STTools_MenuControl * menuControl = [[STTools_MenuControl alloc] initWithFrame:CGRectMake(0, stoneobs_ui_label.bottom+30, 65, 65+30) imageName:@"微信" title:@"微信好友" titleColor:STONEOBS_UI_FIRST_TEXT_COLOR imageHeight:65 lableHeight:30];
    [menuControl setOnSelctedControl:^(STTools_MenuControl *control) {
        
        [TMWechatSocietyManger.manger shareToWeChatText:@"https://www.pgyer.com/E2so" toFriendCirle:NO handle:^(BOOL success, NSString *alert) {
            
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"分享成功"];
            }else{
                [SVProgressHUD showErrorWithStatus:alert];
            }
            [self send_did_share_request];
        }];
        [weakSelf stoneobs_fun_dismiss];
    }];
    menuControl.right = UISCREEN_WIDTH  / 2 -10;
    [self.stoneobs_ui_customView addSubview:menuControl];

    STTools_MenuControl * cicleControl = [[STTools_MenuControl alloc] initWithFrame:CGRectMake(0, stoneobs_ui_label.bottom+30, 65, 65+30) imageName:@"朋友圈" title:@"朋友圈" titleColor:STONEOBS_UI_FIRST_TEXT_COLOR imageHeight:65 lableHeight:30];
    [cicleControl setOnSelctedControl:^(STTools_MenuControl *control) {
        
        NSString * url = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.url;
        if (!url.length) {
            url = @"https://www.pgyer.com/E2so";
        }
        [TMWechatSocietyManger.manger shareToWeChatText:url toFriendCirle:YES handle:^(BOOL success, NSString *alert) {
            
        }];
        [weakSelf stoneobs_fun_dismiss];
    }];
    cicleControl.left = UISCREEN_WIDTH  / 2 +10;
    [self.stoneobs_ui_customView addSubview:cicleControl];
    
    
    self.stoneobs_ui_customView.height = cicleControl.bottom + 50 + self.stoneobs_ui_customView.layer.cornerRadius ;
    if (@available(iOS 11.0, *)) {
        self.stoneobs_ui_customView.height = self.stoneobs_ui_customView.height + UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom;
    } else {
        // Fallback on earlier versions
    }
    
    
    self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
    
    
}

- (void)stoneobs_fun_show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
        self.stoneobs_ui_customView.bottom = UISCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)stoneobs_fun_dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
        self.stoneobs_ui_clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)stoneobs_fun_dismissWithHandle:(void(^)(void))handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.stoneobs_ui_customView.top = UISCREEN_HEIGHT;
        self.stoneobs_ui_clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
- (void)send_did_share_request{

    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    NSString * stoneobs_net_net_url;
    if (self.good_id.length) {
        [stoneobs_net_param_dic setObject:self.good_id forKey:@"id"];
        stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/goods/shareCallback"];
    }
    if (self.tie_id.length) {
        [stoneobs_net_param_dic setObject:self.tie_id forKey:@"id"];
        stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/posts/shareCallback"];
    }

    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
